package org.shj.sso.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketClient {

	public static void main(String[] args){
		try(Socket socket = new Socket("127.0.0.1", 9999)){
			OutputStream outputStream = socket.getOutputStream();

            DataOutputStream dataOutputStream =new DataOutputStream(outputStream);
			int type = 1;
			byte[] data = "helooa工".getBytes();
			int len = data.length;
			dataOutputStream.writeByte(type);
			dataOutputStream.writeInt(len);
			dataOutputStream.write(data);
			dataOutputStream.flush();
			
			/*BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			String msg = "hello ";
			bw.write(msg);
			bw.flush();*/
			socket.shutdownOutput();
			
			InputStream inputStream = socket.getInputStream();
            DataInputStream dataInputStream =new DataInputStream(inputStream);
            byte b = dataInputStream.readByte();
            len = dataInputStream.readInt();
            data =new byte[len];
            dataInputStream.readFully(data);
            String str =new String(data);
            System.out.println("接收到返回的数据类型为："+b);
            System.out.println("接收到返回的数据长度为："+len);
            System.out.println("接收到返回的数据内容为："+str);
			socket.close();
		}catch(Exception e){
			
		}finally{
			
		}
	}
}
